package own.stu.jobgib.playown.util;

import com.google.gson.Gson;
import com.google.gson.internal.$Gson$Types;
import com.google.gson.reflect.TypeToken;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.List;

public abstract class ResultCallBack<T> {
    public Type mType;

    public ResultCallBack() {
        mType = getSuperclassTypeParameter(getClass());
    }

    public Type getSuperclassTypeParameter(Class<?> subclass) {
        Type superClass = subclass.getGenericSuperclass();
        if (superClass instanceof Class) {
            throw new RuntimeException("Missing type parameter.");
        }
        ParameterizedType parameterized = (ParameterizedType) superClass;
        return $Gson$Types
                .canonicalize(parameterized.getActualTypeArguments()[0]);
    }

    public Class getClassType() {
        return getClass();
    }

    static class BBQ{
        private int id;
        private String name;
    }
    public static void main(String[] args){
        String jsonData = "{\"id\": 1, \"name\": \"黄磊\"}";
        String jsonArray = "[{\"id\": 1, \"name\": \"黄磊\"},{\"id\": 2, \"name\": \"黄磊2号\"},{\"id\": 3, \"name\": \"黄磊3号\"}]";

        Gson gson = new Gson();
        BBQ bbq = gson.fromJson(jsonData, BBQ.class/*new TypeToken<BBQ>(){}.getType()*/);
        System.out.println("id=" + bbq.id);
        System.out.println("name=" + bbq.name);

        Type type = new TypeToken<List<BBQ>>(){}.getType();
        List<BBQ> bbqs = gson.fromJson(jsonArray, type);
        System.out.println("id2号=" + bbqs.get(0).id);
        System.out.println("name2号=" + bbqs.get(0).name);

        System.out.println(new ResultCallBack<List<BBQ>>(){}.mType.getTypeName());
    }
}